﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities.Kml;
using PPIDesktopClient.General;
using PPIDesktopClient.Interface;

namespace PPIDesktopClient.Entities
{
    public class EBasePosition 
    {
        #region Private Members
        double m_Latitude;
        double m_Longitude;
        double m_Altitude;
        Int32 m_SatelliteCount;
        DateTime m_SatelliteTime;
        bool m_SolutionAvailable;
        EHandheld m_Handheld;
        DateTime m_Date;
        string m_Address;
        string m_Icon;
        private EUtility_QueryAssetTextShow m_TextToShow;
        #endregion

        #region Properties
        public double Latitude
        {
            get { return m_Latitude; }
            set { m_Latitude = value; }
        }
        public double Longitude
        {
            get { return m_Longitude; }
            set { m_Longitude = value; }
        }
        public double Altitude
        {
            get { return m_Altitude; }
            set { m_Altitude = value; }
        }
        public Int32 SatelliteCount
        {
            get { return m_SatelliteCount; }
            set { m_SatelliteCount = value; }
        }
        public DateTime SatelliteTime
        {
            get { return m_SatelliteTime; }
            set { m_SatelliteTime = value; }
        }
        public bool SolutionAvailable
        {
            get { return m_SolutionAvailable; }
            set { m_SolutionAvailable = value; }
        }
        public EHandheld Handheld
        {
            get { return m_Handheld; }
            set { m_Handheld = value; }
        }
        public DateTime Date
        {
            get { return m_Date; }
            set { m_Date = value; }
        }
        public string Address
        {
            set
            {
                m_Address = value;
            }
        }

        public string LatitudeTitle
        {
            get 
            { 
                if(Latitude == Double.MinValue)
                    return "-";
                else
                    return Latitude.ToString("F6");
            }
        }
        public string LongitudTitle
        {
            get
            {
                if (Longitude == Double.MinValue)
                    return "-";
                else
                    return Longitude.ToString("F6");
            }
        }
        public string AltitudTitle
        {
            get
            {
                if (Altitude == Double.MinValue)
                    return "-";
                else
                    return Altitude.ToString("F6");
            }
        }
        public string SatelliteCountTitle
        {
            get
            {
                if (SatelliteCount == Int32.MinValue)
                    return "-";
                else
                    return SatelliteCount.ToString();
            }
        }
        public string Icon
        {
            get
            {
                return m_Icon;
            }
            set
            {
                m_Icon = value;
            }
        }
        public EUtility_QueryAssetTextShow TextToShow
        {
            get
            {
                return m_TextToShow;
            }
            set
            {
                m_TextToShow = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Generate xml code to show de position
        /// </summary>
        /// <returns></returns>
        public string GetPositionKML()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Point id=\"test1\">");
            s.AppendLine("  <coordinates>" + m_Longitude.ToString().Replace(",", ".") + "," + m_Latitude.ToString().Replace(",", ".") + " ," + m_Altitude.ToString().Replace(",", ".") + "</coordinates>");
            s.AppendLine("</Point>");
            return s.ToString();
        }

        /// <summary>
        /// Get the Icon depends the color
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        protected static string GetPositionIcon(string color)
        {
            if (string.IsNullOrEmpty(color))
                return "http://www.dev-poleposition.com/IconGoogleEarthClassesI.jpg";
            else
                return "http://www.dev-poleposition.com/Images/IconGoogleEarth_" + color + ".png";
        }

        public virtual string GetHtmlDescription()
        {
            throw new NotImplementedException();
        }

        protected virtual string GetHtmlName()
        {
            throw new NotImplementedException();
        }

        protected string GetHtmlPlacemark()
        {
            throw new NotImplementedException();
        }

        protected virtual string GetPlacemarkId()
        {
            throw new NotImplementedException();
        }

        protected virtual string GetPlaceMark()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Placemark id=\""+GetPlacemarkId()+"\">");
            s.AppendLine(" <Style>");
            s.AppendLine("     <BalloonStyle>");
            s.AppendLine("         <color>F9DDCD</color>");
            s.AppendLine("         <text>$[description]</text>");
            s.AppendLine("     </BalloonStyle>");
            s.AppendLine(" <IconStyle>");
            s.AppendLine("     <Icon>");
            s.AppendLine("         <href>" + GetIcon() + "</href>");
            s.AppendLine("     </Icon>");
            s.AppendLine(" </IconStyle>");
            s.AppendLine("</Style>");
            s.AppendLine(" <name>" + GetHtmlName() + "</name>");
            s.AppendLine(" <description>");
            s.AppendLine("     <![CDATA[" + GetHtmlDescription() + "]]>");
            s.AppendLine(" </description>");
            s.AppendLine(GetPositionKML());
            s.AppendLine("</Placemark>");
            return s.ToString();
        }

        protected virtual string GetIcon()
        {
            return GetPositionIcon(m_Icon);
        }

        protected virtual EUtility_QueryAssetTextShow GetTextToShow()
        {
            return m_TextToShow;
        }

        public EKmlFile ToKml()
        {
            EKmlFile _file = new EKmlFile();
            _file.KMLCode = GetPlaceMark();
            return _file;
        }
        #endregion

        public EBasePosition()
        {
            m_Latitude = Double.MinValue;
            m_Longitude = Double.MinValue;
            m_Altitude = Double.MinValue;
            m_Address = string.Empty;
            m_Icon = "Red";
            m_TextToShow = EUtility_QueryAssetTextShow.Internal_Utility_ID;
            m_Handheld = new EHandheld();
        }

        public string GetAddress()
        {
            if (string.IsNullOrEmpty(m_Address))
            {
                return Latitude.ToString().Replace(",", ".") + "," + Longitude.ToString().Replace(",", ".");
            }else
            {
                return m_Address;
            }
        }

    }
}
